package com.wpw.dream.jsoup;

import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;
import org.junit.Test;

public class JsoupTest {

	@Test
	public void HtmlTest() {
		// String html = "<html><head><title>First parse</title></head>"
		// + "<body><p>Parsed HTML into a doc.</p></body></html>";
		String html = "<p>Parsed HTML into a doc.</p>";

		Document doc = Jsoup.parse(html);
		Elements allElements = doc.getAllElements();
		for (Element element : allElements) {
			System.out.println(element);
		}
	}

	@Test
	public void parseBodyFragmentTest() {
		String html = "<div><p>Lorem ipsum.</p>";
		Document doc = Jsoup.parseBodyFragment(html);
		Element body = doc.body();
		System.out.println(body);
	}

	@Test
	public void whiteListTest() {
		String html = "<p>Parsed HTML into a doc.</p><script>alert(11);</scripte>";

		Document doc = Jsoup.parse(html);
		Elements allElements = doc.getAllElements();
		Whitelist w = new Whitelist();
		String clean = Jsoup.clean(html, w);
		System.out.println(clean);
		
	}
	
	@Test
	public void urlGetTest() throws IOException {
		Connection connect = Jsoup.connect("https://www.cnblogs.com/");
		Response execute = connect.validateTLSCertificates(false).execute();
		Document doc = connect.post();
		//Document doc = connect.get();
		String title = doc.title();
		System.out.println(title);
	}
	
	@Test
	public void GetBokey() throws IOException {
		//http://www.cnblogs.com/
		Connection connect = Jsoup.connect("http://huizhan.mysteel.com/huodong");
		Response execute = connect.execute();
		Document doc = connect.get();
		Elements titleLnk = doc.getElementsByClass("m_explainTie");
		for (Element element : titleLnk) {
//			String link = element.getElementsByTag("a").attr("abs:href");
			String link = element.getElementsByTag("a").get(0).absUrl("href");
			String text = element.text();
			System.out.println(text + ":" + link);
		}
		
	}
}
